﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views.Base;
using WinForm.Controls;
using WinForm.Details;
using WinForm.Properties;

namespace WinForm.Modules
{
    public partial class ModuleBase : UserControl
    {
        public event EventHandler ActiveDetailChanged;

        #region 字段

        private DetailBase _activeDetailControl;

        #endregion

        #region  属性

        /// <summary>
        /// 模块的当前视图
        /// </summary>
        protected virtual ColumnView CurrentView
        {
            get { return MainView; }
        }

        /// <summary>
        /// 模块的主要视图
        /// </summary>
        protected virtual ColumnView MainView
        {
            get
            {
                List<Control> ctls = Controls.OfType<ucGridControl>().Cast<Control>().ToList();
                if (ctls.Count != 1)
                {
                    return null;
                }
                var gridControl = (ucGridControl) ctls[0];
                return gridControl.gridView1;
            }
        }

        /// <summary>
        /// 该模块包含的子模块控件
        /// </summary>
        public List<DetailBase> DetailControls { get; private set; }

        /// <summary>
        /// 当前子模块控件
        /// </summary>
        public DetailBase ActiveDetailControl
        {
            get { return _activeDetailControl; }
            set
            {
                if (_activeDetailControl != value)//==value 时候什么都不用做
                {
                    //todo:做好当前模块变更时的准备工作，然后变更，然后更新page等
                    _activeDetailControl = value;
                    //UpdateActiveDetailControl();
                }
            }
        }

        /// <summary>
        /// 删除时提示数据所在的列
        /// </summary>
        private int DeleteTipColumn { get; set; }

        #endregion

        #region 构造

        public ModuleBase()
        {
            InitializeComponent();
        }

        public ModuleBase(int column)
        {
            InitializeComponent();
            DeleteTipColumn = column;
        }

        #endregion


        #region 方法
        

        /// <summary>
        /// 添加一个新的子模块控件
        /// </summary>
        /// <param name="item"></param>
        public void AddDetailControl(DetailBase item)
        {
            DetailControls.Add(item);
        }

        /// <summary>
        /// 移除一个子模块控件
        /// </summary>
        /// <param name="item"></param>
        public void RemoveDetailControl(DetailBase item)
        {
            DetailControls.Remove(item);
        }
        

        /// <summary>
        /// 添加一条记录
        /// </summary>
        public void Add()
        {
            AddData();
        }

        /// <summary>
        /// 编辑一条记录
        /// </summary>
        public void Edit()
        {
            EditData();
        }

        /// <summary>
        /// 删除一条记录
        /// </summary>
        public void Delete()
        {
            if (MessageBox.Show(string.Format("确实要删除 {0} 吗？", MainView.GetRowCellDisplayText(MainView.FocusedRowHandle, MainView.Columns[DeleteTipColumn])), Resources.QuestionCaption, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) 
                return;
            DeleteData();
            RefreshData();
        }
        #endregion

        #region 虚方法
        /// <summary>
        /// 第一条记录
        /// </summary>
        protected internal virtual void First()
        {
            if (CurrentView == null) return;

            //CurrentView.FocusedRowHandle++;
            CurrentView.MoveFirst();
        }

        /// <summary>
        ///  上一条记录
        /// </summary>
        protected internal virtual void Previous()
        {
            if (CurrentView == null) return;

            CurrentView.FocusedRowHandle--;
        }

        /// <summary>
        /// 下一条记录
        /// </summary>
        protected internal virtual void Next()
        {
            if (CurrentView == null) return;

            //CurrentView.FocusedRowHandle++;
            CurrentView.MoveNext();
        }


        /// <summary>
        /// 末一条记录
        /// </summary>
        protected internal virtual void Last()
        {
            if (CurrentView == null) return;

            //CurrentView.FocusedRowHandle++;
            CurrentView.MoveLast();
        }
        //protected virtual void BeginRefreshData()
        //{
        //    Cursor.Current = Cursors.WaitCursor;
        //    oldSession = Session;
        //    if (Session != null)
        //        session = new UnitOfWork(Session.DataLayer);
        //}
        //protected virtual void EndRefreshData()
        //{
        //    if (oldSession != null)
        //    {
        //        oldSession.Dispose();
        //        oldSession = null;
        //    }
        //}

        #region 视图

        /// <summary>
        /// 大图标
        /// </summary>
        public virtual void LargeIconView()
        {
        }

        /// <summary>
        /// 大图标
        /// </summary>
        public virtual void SmallconView()
        {
        }

        /// <summary>
        /// 列表
        /// </summary>
        public virtual void ListView()
        {
        }

        /// <summary>
        /// 详细
        /// </summary>
        public virtual void DetailsView()
        {
        }

        /// <summary>
        /// 平铺
        /// </summary>
        public virtual void TileView()
        {
        }

        #endregion


        #endregion

        #region ChangActiveDetail

        public void ChangActiveDetail(DetailBase module)
        {
            ChangActiveDetail(module, false);
        }
        public void ChangActiveDetail(DetailBase module, bool readOnly)
        {
            if (module == null) return;

            //TODO：考虑当前子模块数据没有保存的情况，若没有保存首先弹出对话框，请用户确认是否保存，然后才。。。

            //释放正在显示的模块
            if (ActiveDetailControl != null)
            {
                ActiveDetailControl.Dispose();
            }

            //修改当前模块为指定模块
            ActiveDetailControl = module;
        }

        /// <summary>
        /// 触发模块改变事件
        /// </summary>
        public void RaiseDetailChanged()
        {
            if (ActiveDetailChanged != null)
                ActiveDetailChanged(this, EventArgs.Empty);
        }

        #endregion


        
        public virtual void AddData()
        {
        }
        public virtual void EditData()
        {
        }
        public virtual void DeleteData()
        {
        }
        public virtual void RefreshData()
        {
            //BeginRefreshData();
            //EndRefreshData();
        }

        private void ModuleBase_Load(object sender, EventArgs e)
        {
            RefreshData();
        }
    }
}
